package com.xinchuang.service.targetSystem.Impl.damage;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.xinchuang.comment.R;
import com.xinchuang.common.Result;
import com.xinchuang.common.ResultUtil;
import com.xinchuang.entity.targetSystem.damage.DictionaryTable;
import com.xinchuang.entity.targetSystem.damage.Missile;
import com.xinchuang.entity.targetSystem.damage.TWarhead;
import com.xinchuang.entity.targetSystem.damage.TWarheadExample;
import com.xinchuang.mapper.targetSystem.damage.TWarheadMapper;
import com.xinchuang.service.targetSystem.damage.TWarheadService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class  TWarheadServiceImpl extends ServiceImpl<TWarheadMapper, TWarhead> implements TWarheadService {

    @Override
    public long countByExample(TWarheadExample example) {
        return baseMapper.countByExample(example);
    }

    @Override
    public int deleteByExample(TWarheadExample example) {
        return baseMapper.deleteByExample(example);
    }

    @Override
    public List<TWarhead> selectByExample(TWarheadExample example) {
        return baseMapper.selectByExample(example);
    }

    @Override
    public int updateByExampleSelective(TWarhead record, TWarheadExample example) {
        return baseMapper.updateByExampleSelective(record, example);
    }

    @Override
    public int updateByExample(TWarhead record, TWarheadExample example) {
        return baseMapper.updateByExample(record, example);
    }

    @Override
    public int updateBatch(List<TWarhead> list) {
        return baseMapper.updateBatch(list);
    }

    @Override
    public int updateBatchSelective(List<TWarhead> list) {
        return baseMapper.updateBatchSelective(list);
    }

    @Override
    public int batchInsert(List<TWarhead> list) {
        return baseMapper.batchInsert(list);
    }

    @Override
    public Result insertOrUpdate(TWarhead record) {
        if (StringUtils.isBlank(record.getWarheadName())) {
            return ResultUtil.error(500, "名称不能为空");
        }
        if(StringUtils.isEmpty(record.getId())){
            record.setId(UUID.randomUUID().toString().replace("-", ""));
            record.setDataStatus(0);
        }
        baseMapper.insertOrUpdate(record);
        return ResultUtil.success();
    }

    @Override
    public int insertOrUpdateSelective(TWarhead record) {
        return baseMapper.insertOrUpdateSelective(record);
    }

    @Override
    public Map<String, Object> queryList(Integer pageNum, Integer pageSize, String warheadName, String warheadType, String missileId) {
        Page<TWarhead> tWarheadPage = new Page<>(pageNum, pageSize);
        MPJLambdaWrapper<TWarhead> wrapper = new MPJLambdaWrapper<TWarhead>();
        wrapper.selectAll(TWarhead.class);
        wrapper.selectAs(DictionaryTable::getLabel,TWarhead::getWarheadTypeName);
        wrapper.selectAs(Missile::getMissileName,TWarhead::getMissileIdName);
        wrapper.leftJoin(DictionaryTable.class,DictionaryTable::getValue,TWarhead::getWarheadType).eq(DictionaryTable::getType,"warhead_type");
        wrapper.leftJoin(Missile.class,Missile::getId,TWarhead::getMissileId);
        if (StringUtils.isNotEmpty(warheadName)){
            wrapper.like(TWarhead::getWarheadName,warheadName);
        }
        if (StringUtils.isNotEmpty(warheadType)){
            wrapper.like(TWarhead::getWarheadType,warheadType);
        }
        if (StringUtils.isNotEmpty(missileId)){
            wrapper.like(TWarhead::getMissileId,missileId);
        }
        wrapper.eq(TWarhead::getDataStatus, 0);
        wrapper.orderByDesc(TWarhead::getUpdateTime);
        Page<TWarhead> pageList = baseMapper.selectPage(tWarheadPage, wrapper);
        //重写定义分页插件返回参数
        Map<String, Object> map = new HashMap<>();
        map.put("dataList", pageList.getRecords());
        map.put("total", pageList.getTotal());
        return map;
    }

    @Override
    public R deleteByIds(List<String> ids, boolean deleteStatus) {
        try {
            int num;
            if (deleteStatus){
                //物理删除
                num = baseMapper.deleteBatchIds(ids);
            }else {
                //逻辑删除
                UpdateWrapper<TWarhead> wrapper =new UpdateWrapper<>();
                wrapper.lambda().set(TWarhead::getDataStatus,1).in(TWarhead::getId,ids);
                num = baseMapper.update(null,wrapper);
            }
            if (num > 0){
                return R.success("删除成功");
            }else {
                return R.fail("删除失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail("删除失败");
        }
    }
}
